package org.apache.oozie.tools.workflowgenerator.client.property.control;

import java.util.ArrayList;

import org.apache.oozie.tools.workflowgenerator.client.property.PropertyTable;
import org.apache.oozie.tools.workflowgenerator.client.widget.NodeWidget;

import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;

public class JoinPropertyTable extends PropertyTable {

    protected Grid grid;

    public JoinPropertyTable(NodeWidget w) {
        super(w);
        initConf();
        initWidget();
    }

    /**
     * Initialize configuration
     */
    private void initConf() {
        widgetList = new ArrayList<NodeWidget>();
        updateWidgetList();
        widgetDropDown = new ArrayList<NodeWidget>();
        updateWidgetDropDown();
    }

    /**
     * Generate xml elements of join node and attach them to xml doc
     */
    public void generateXML(Document doc, Element root, NodeWidget next) {

        Element action = doc.createElement("join");
        action.setAttribute("name", name.getText());
        action.setAttribute("to", next.getName());
        root.appendChild(action);

    }

    /**
     * Initialize widgets shown in property table
     */
    protected void initWidget() {

        grid = new Grid(2, 2);
        this.add(grid);

        this.setAlwaysShowScrollBars(true);
        this.setSize("100%", "80%");

        // insert row for name
        grid.setWidget(0, 0, createLabel("Name"));
        name = new TextBox();
        grid.setWidget(0, 1, formatCell(name));

        // insert row for OK
        grid.setWidget(1, 0, createLabel("To"));
        okVal = new ListBox();
        createWidgetDropDownList(okVal);
        grid.setWidget(1, 1, formatCell(okVal));

    }
}
